home *** CD-ROM | disk | FTP | other *** search
/ Die Speccy' 97 / Die Speccy' 97.iso / amiga_system / the_aminet / util / shell / csh548src.lha / WindowBounds.c < prev    next >
C/C++ Source or Header  |  1995-01-07  |  2KB  |  123 lines

  1. /*
  2.  *  get window bounds (dimensions)
  3.  *  Copyright 1994-1995, Andreas M. Kirchwitz
  4.  */
  5.  
  6.  
  7. #include <exec/types.h>
  8. #include <dos/dos.h>
  9. #include <dos/dosextens.h>
  10. #include <clib/dos_protos.h>
  11. #include <clib/exec_protos.h>
  12. #include <proto/dos.h>
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #include <string.h>
  16.  
  17. static unsigned char aWSR[] = {0x9b, 0x30, 0x20, 0x71, 0};        /* Window Status Request */
  18. static unsigned char aWBR[] = {0x9b, 0x31, 0x3b, 0x31, 0x3b, 0};    /* Window Bounds Report */
  19.  
  20. #define BUF_LEN            40
  21.  
  22.  
  23.  
  24. void get_WindowBounds_def(int *width, int *height)
  25. {
  26.     *width  = 80;
  27.     *height = 24;
  28. }
  29.  
  30.  
  31.  
  32. long get_WindowBounds_env(int *width, int *height)
  33. {
  34.     long got_wb = 0;
  35.     unsigned char iline[BUF_LEN + 1];
  36.  
  37.     if (GetVar ("COLUMNS", iline, BUF_LEN, NULL) > 0L) {
  38.         *width = atoi(iline);
  39.         got_wb = 1;
  40.     }
  41.  
  42.     if (GetVar ("LINES", iline, BUF_LEN, NULL) > 0L) {
  43.         *height = atoi(iline);
  44.         got_wb = 1;
  45.     }
  46.  
  47.     return(got_wb);
  48. }
  49.  
  50.  
  51.  
  52. long get_WindowBounds_con(BPTR con, int *width, int *height,long timeout)
  53. {
  54.     long got_wb = 0;
  55.     unsigned char *p1, *p2, *p3;
  56.     int i = 0, start = 0;
  57.     unsigned char iline[BUF_LEN + 1];
  58.  
  59.     if (!con)
  60.         return(got_wb);
  61.  
  62.     if (!IsInteractive(con))
  63.         return(got_wb);
  64.  
  65.     Write (con, aWSR, strlen (aWSR));
  66.  
  67.     /* now fill buffer, start with beginning of WBR */
  68.  
  69.     while (i < BUF_LEN && WaitForChar (con, timeout)) {
  70.         Read (con, &iline[i], 1);
  71.         if (start)
  72.             i++;
  73.         else if (iline[i] == aWBR[0]) {
  74.             i++;
  75.             start = 1;
  76.         }
  77.     }
  78.  
  79.     iline[i] = 0;
  80.  
  81.     if (i > 0) {
  82.         if (p1 = strstr (iline, aWBR)) {
  83.             p1 += strlen (aWBR);
  84.             if (p2 = strchr (p1, ';')) {
  85.                 *p2 = 0;
  86.                 ++p2;
  87.                 if (p3 = strchr (p2, 'r')) {
  88.                     *p3 = 0;
  89.  
  90.                     *height = atoi (p1);
  91.                     *width = atoi (p2);
  92.  
  93.                     got_wb = 1;
  94.                 }
  95.             }
  96.         }
  97.     }
  98.  
  99.     /*
  100.         9b 31 3b 31 3b <height> 3b <width> 20 72
  101.      */
  102.  
  103.     return(got_wb);
  104. }
  105.  
  106.  
  107.  
  108. long get_WindowBounds_Output(int *width, int *height,long timeout)
  109. {
  110.     long got_wb = 0;
  111.  
  112.     if (Output()) {
  113.         if (IsInteractive(Output())) {
  114.             SetMode(Output(),1);    /* raw */
  115.             got_wb = get_WindowBounds_con(Output(),width,height,timeout);
  116.             SetMode(Output(),0);    /* cooked */
  117.         }
  118.     }
  119.  
  120.     return(got_wb);
  121. }
  122.  
  123.